layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var Bridge = {
        tableId: "bridgeTable",    //表格id
        condition: {
            bridgeName: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Bridge.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'bridgeId', hide: true, sort: true, title: '桥梁id'},
            {field: 'bridgeName', sort: true, title: '名称'},
            {field: 'line', sort: true, title: '线路'},
            {field: 'stakeCode', sort: true, title: '桩号'},
            {field: 'span', sort: true, title: '桥梁跨径'},
            {field: 'coordinate', sort: true, title: '坐标'},
            {field: 'beamNo', sort: true, title: '梁编号'},
            {field: 'deviceNo', sort: true, title: '物联网标识'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Bridge.search = function () {
        var queryData = {};
        queryData['condition'] = $("#bridgeName").val();
        table.reload(Bridge.tableId, {where: queryData});
    };

    /**
     * 弹出添加角色
     */
    Bridge.openAddRole = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加桥梁',
            area: ['800px', '500px'],
            content: Feng.ctxPath + '/bridge/bridge_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Bridge.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Bridge.exportExcel = function () {
        var checkRows = table.checkStatus(Bridge.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑桥梁
     *
     * @param data 点击按钮时候的行数据
     */
    Bridge.onEditBridge = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改桥梁',
            area: ['800px', '500px'],
            content: Feng.ctxPath + '/bridge/bridge_edit?bridgeId=' + data.bridgeId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Bridge.tableId);
            }
        });
    };

    /**
     * 点击删除桥梁
     *
     * @param data 点击按钮时候的行数据
     */
    Bridge.onDeleteBridge = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/bridge/remove", function () {
                Feng.success("删除成功!");
                table.reload(Bridge.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("bridgeId", data.bridgeId);
            ajax.start();
        };
        Feng.confirm("是否删除桥梁 " + data.bridgeName + "?", operation);
    };



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Bridge.tableId,
        url: Feng.ctxPath + '/bridge/page',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Bridge.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Bridge.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Bridge.openAddRole();
    });

    // 工具条点击事件
    table.on('tool(' + Bridge.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Bridge.onEditBridge(data);
        } else if (layEvent === 'delete') {
            Bridge.onDeleteBridge(data);
        }
    });
});
